package com.zz.check.ui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.HashSet;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.zz.check.util.JarConflictDetectUtil;

/**
 * 软件界面
 * @author zys
 *
 */
public class JarDetectAPP {

	private JFrame frmJar;
	private JTextField jarDir;
	private JTextArea resultArea;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JarDetectAPP window = new JarDetectAPP();
					window.frmJar.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public JarDetectAPP() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmJar = new JFrame();
		frmJar.setTitle("jar包检测");
		frmJar.setResizable(false);
//		frmJar.setBounds(300, 100, 600, 600);
		frmJar.setSize(643, 500);
		frmJar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmJar.getContentPane().setLayout(null);
		frmJar.setLocationRelativeTo(null);//居中显示
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(5, 5, 629, 93);
		frmJar.getContentPane().add(panel);
		
		JCheckBox checkBox = new JCheckBox("全部");
		checkBox.setBounds(20, 4, 60, 23);
		panel.add(checkBox);
		
		JCheckBox dependChk = new JCheckBox("依赖问题");
		dependChk.setToolTipText("检测jar包间的循环依赖问题(存在循环依赖往往说明设计不良)");
		dependChk.setBounds(95, 4, 78, 23);
		panel.add(dependChk);
		
		JCheckBox chckbxJar = new JCheckBox("冲突问题");
		chckbxJar.setToolTipText("检测jar包冲突问题(不同jar含有相同类即为jar包冲突，可能引起诡异的系统问题)");
		chckbxJar.setBounds(173, 4, 78, 23);
		panel.add(chckbxJar);
		
		JCheckBox missChk = new JCheckBox("缺失问题");
		missChk.setToolTipText("检测是否存在类文件缺失或类方法缺失问题");
		buttonGroup.add(missChk);
		missChk.setBounds(252, 4, 78, 23);
		panel.add(missChk);
		
		JCheckBox compileChk = new JCheckBox("编译问题");
		compileChk.setToolTipText("检测是否存在编译时jdk版本高于运行时jdk版本的jar包");
		compileChk.setBounds(331, 4, 78, 23);
		panel.add(compileChk);
		
		
		jarDir = new JTextField("请输入或选择jar包所在目录");
		jarDir.setBounds(20, 29, 428, 21);
		panel.add(jarDir);
		jarDir.setColumns(10);
		jarDir.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				jarDir.setText("");
				resultArea.setText("");
			}
			public void focusLost(FocusEvent e) {}
		});
		
		resultArea = new JTextArea(); 
		resultArea.setRows(18);
		resultArea.setBounds(5, 10, 200, 374);
		resultArea.setColumns(70);
		resultArea.setTabSize(80);
		resultArea.setWrapStyleWord(true);
		
		JScrollPane scr = new JScrollPane(resultArea);
		scr.setLocation(5, 103);
		scr.setSize(629, 359);
		frmJar.getContentPane().add(scr);
		
		scr.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
		scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

		
		JButton fileSelBtn = new JButton("选择");
		fileSelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser.setDialogTitle("请选择jar包目录");
				int result=chooser.showOpenDialog(frmJar);
				if (result == JFileChooser.APPROVE_OPTION) {
					File file=chooser.getSelectedFile();
					jarDir.setText(file.toString());
					doJarDetect();
				}
			}
		});
		fileSelBtn.setBounds(458, 28, 63, 23);
		panel.add(fileSelBtn);
		
		JButton exportBtn = new JButton("导出");
		exportBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				byte[] bytes=resultArea.getText().getBytes();
				if(bytes.length==0){
					return ;
				}
				
				JFileChooser chooser = new JFileChooser();  
	            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
	            chooser.setDialogType(JFileChooser.SAVE_DIALOG);  
	            chooser.setMultiSelectionEnabled(false);  
	            chooser.setAcceptAllFileFilterUsed(false);  
	            File file = new File("result.txt");
	            chooser.setSelectedFile(file);
	            int result=chooser.showSaveDialog(frmJar);
	            if (result == JFileChooser.APPROVE_OPTION) {
	            	file = chooser.getSelectedFile();
	            	try {
						
						FileOutputStream out=new FileOutputStream(file);
						out.write(bytes, 0, bytes.length);
						out.flush();
						out.close();
						JOptionPane.showMessageDialog(frmJar, "结果导出成功！");
					} catch (Exception e1) {
						e1.printStackTrace();
					}
	            }
			}
		});
		exportBtn.setBounds(556, 70, 60, 23);
		panel.add(exportBtn);
		
		JLabel label = new JLabel("检测结果:");
		label.setBounds(3, 75, 74, 15);
		panel.add(label);
		
		JButton detectBtn = new JButton("执行检测");
		detectBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doJarDetect();
			}
		});
		detectBtn.setBounds(526, 28, 93, 23);
		panel.add(detectBtn);
		
		JButton clearBtn = new JButton("清空");
		clearBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resultArea.setText("");
			}
		});
		clearBtn.setBounds(488, 70, 60, 23);
		panel.add(clearBtn);
		
		
	}
	
	private void doJarDetect(){
		String filePath = jarDir.getText();
		File file = new File(filePath);
		if(!file.isDirectory()){
			JOptionPane.showMessageDialog(frmJar, "请输入或选择正确的jar包目录！");
			return;
		}
		HashMap<String, HashSet<String>> hashtable=JarConflictDetectUtil.getConflict(filePath);
		if(hashtable.size()==0){
			resultArea.setText("该目录下无jar包冲突");
		}else{
			String ret="";
			for(String jarname:hashtable.keySet()){
				ret+=jarname+"\n"+hashtable.get(jarname).toString()+"\n";
			}
			resultArea.setText(ret);
//			System.out.println(ret);
		}
	}
}
